package com.mdcorbri.helicoptobot.mediators.editView
{
	
	
	import com.mdcorbri.helicoptobot.mediators.AbstractViewMediator;
	import com.mdcorbri.helicoptobot.mediators.getItems.GetItemsViewMediator;
	import com.mdcorbri.helicoptobot.model.ApplicationModel;
	import com.mdcorbri.helicoptobot.views.editView.EditView;
	import com.mdcorbri.helicoptobot.vo.ItemVO;
	import com.mdcorbri.helicoptobot.vo.ProjectVO;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import mx.binding.utils.*;
	import mx.collections.ArrayCollection;
	import mx.controls.ComboBox;
	import mx.core.UIComponent;
	import mx.events.DynamicEvent;
	import mx.events.ListEvent;
	import mx.utils.UIDUtil;
	
	import nz.co.codec.flexorm.criteria.Criteria;
	
	
	public class EditViewMediator extends AbstractViewMediator
	{
		
		private var dynamicEvent:DynamicEvent;
		
		private var _projectVO:ProjectVO;
	
		/**
		 * Backing variable for <code>appModel</code> setter.
		 */
		[Autowire]
		public var applicationModel:ApplicationModel;
		
		[Autowire]
		public var getItemsViewMediator:GetItemsViewMediator;
		
		
		public function EditViewMediator()
		{
			super();
		}
		
		/**
		 * 
		 */
		public function get view():EditView
		{
			return this._view as EditView;
		}
		
		
		/** ************************************************************************
		 *                overrides
		 *  ************************************************************************/
		 
		override protected function setViewListeners():void
		{
			super.setViewListeners();
			
			this.view.btnSubmitProject.addEventListener(MouseEvent.CLICK, handleBtnSubmitProjectClick);
			
			this.view.cmbxProjects.addEventListener(ListEvent.CHANGE, handleCmbxProjectsChange);
			
			this.view.btnSubmitItem.addEventListener(MouseEvent.CLICK, handleBtnSubmitItemClick);
			
			this.view.txtAreaItem.addEventListener(TextEvent.TEXT_INPUT, handleTxtAreaItemTextInput);
		}
		
		
		/**
		 * 
		 * BindingUtils.bindProperty( target, "targetProperty", source, "sourceProperty" );
		 */		
		override protected function setViewDataBindings():void
		{
			super.setViewDataBindings();
			BindingUtils.bindProperty(view.cmbxProjects, "dataProvider", getProjects(), "source");
			this.view.cmbxProjects.selectedIndex = -1;
		}
		
		override protected function setViewSecurity():void
		{
			super.setViewSecurity();
			
		}
		
		/** ************************************************************************
		 *                mediators
		 *  ************************************************************************/
		 
		 
		 
		 
		
		/** ************************************************************************
		 *                handlers
		 *  ************************************************************************/
		 
		 /**
		  * 
		  */
		private function handleBtnSubmitProjectClick(mouseEvent:MouseEvent):void
		{
			var projectVO:ProjectVO = new ProjectVO();
			projectVO.projectName = this.view.txtInProjectName.text;
			projectVO.projectUUID = UIDUtil.createUID();
			
			this.view.txtInProjectName.text = "";
			
			applicationModel.entityManager.save(projectVO);
			this.setViewDataBindings();
			
			view.btnSubmitItem.enabled = false;
		}
		
		/**
		 * 
		 */
		private function handleTxtAreaItemTextInput(textEvent:TextEvent):void
		{
			this.view.btnSubmitItem.enabled = true;
		}
		
		/**
		 * 
		 */
		private function handleCmbxProjectsChange(listEvent:ListEvent):void
		{
			var projectName:String = ComboBox(listEvent.currentTarget).selectedLabel;
			
			var fetch:ArrayCollection;
			
			fetch = getProjects(projectName);
			_projectVO = fetch.getItemAt(0) as ProjectVO;
			
			applicationModel.projectVO = _projectVO;
			
			applicationModel.projectItems.source = getProjectItems(_projectVO.projectUUID).source;
			
			
		}
		
		 /**
		  * 
		  */
		private function handleBtnSubmitItemClick(mouseEvent:MouseEvent):void
		{
			var item:String = view.txtAreaItem.text;
			
			var itemVO:ItemVO = new ItemVO();
			itemVO.projectItem = item;
			itemVO.projectUUID = this._projectVO.projectUUID;
			
			applicationModel.entityManager.save(itemVO);
			
			this.view.txtAreaItem.text = "";
			this.view.btnSubmitItem.enabled = false;
			
			applicationModel.projectItems.source = getProjectItems(_projectVO.projectUUID).source;
			
			
		}
		
		
		 
		 /** ************************************************************************
		 *                utils
		 *  ************************************************************************/
		 private function getPoint(uiComponent:UIComponent):void
		 {
		 	trace(uiComponent.id + ' x="' + uiComponent.x + '" y="' + uiComponent.y + '"');
		 }
		 
		 /**
		  * 
		  */		 
		 private function getProjects(value:String="%"):ArrayCollection
		 {
		 	var criteria:Criteria = applicationModel.entityManager.createCriteria(ProjectVO);
			criteria.addLikeCondition("projectName",value);
				
			return applicationModel.entityManager.fetchCriteria(criteria);
		 }
		 
		 /**
		  * 
		  */
		 private function getProjectItems(value:String):ArrayCollection
		 {
		 	var criteria:Criteria = applicationModel.entityManager.createCriteria(ItemVO);
			criteria.addLikeCondition("projectUUID",value);
				
			return applicationModel.entityManager.fetchCriteria(criteria);
		 }
		
		/**
		 * Remove any listeners we've created.
		 */
		override protected function cleanup( event:Event ):void
		{
			super.cleanup(event);
		}

	}
}